package com.jason.mychartstest.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.jason.mychartstest.R;



/**
 * 数据中心的图表折线图, 继承MPChart的折线图
 */
public class CYDataLineChart extends LineChart {

    @SuppressWarnings("unused")
    private static final String TAG = "DEBUG-WCL: " + CYDataLineChart.class.getSimpleName();

    // 默认构造器
    public CYDataLineChart(Context context) {
        super(context);
    }

    public CYDataLineChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CYDataLineChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    // 获取高亮点坐标
    public float[] getHighLightPos(Entry e, Highlight highlight) {
        return getMarkerPosition(e, highlight);
    }

    // 重写这个方法, 修复Bug
    @Override
    protected void drawMarkers(Canvas canvas) {

        // if there is no marker view or drawing marker is disabled
        if (mMarkerView == null || !mDrawMarkerViews || !valuesToHighlight())
            return;

        Rect newRect = canvas.getClipBounds();
        newRect.inset(-80, 0);  //make the rect larger

        canvas.clipRect(newRect, Region.Op.REPLACE);

        //noinspection ForLoopReplaceableByForEach
        for (int i = 0; i < mIndicesToHighlight.length; i++) {

            Highlight highlight = mIndicesToHighlight[i];
            int xIndex = highlight.getXIndex();

            if (xIndex <= mDeltaX && xIndex <= mDeltaX * mAnimator.getPhaseX()) {

                Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]);

                // make sure entry not null
                if (e == null || e.getXIndex() != mIndicesToHighlight[i].getXIndex())
                    continue;

                float[] pos = getMarkerPosition(e, highlight);

                // Marker偏移
                float tmpY = pos[1] - 8 /** AppUtils.getPerDp()*/;

                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setAntiAlias(true);
                paint.setStrokeWidth(5);

                // noinspection deprecation
                paint.setColor(getResources().getColor(R.color.out_circle_color));
                canvas.drawCircle(pos[0], pos[1], 2 /** AppUtils.getPerDp()*/, paint);

                // check bounds
                if (!mViewPortHandler.isInBounds(pos[0], tmpY))
                    continue;

                mMarkerView.refreshContent(e, highlight);

                mMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                mMarkerView.layout(0, 0, mMarkerView.getMeasuredWidth(),
                        mMarkerView.getMeasuredHeight());

                if (tmpY - mMarkerView.getHeight() <= 0) {
                    float y = mMarkerView.getHeight() - tmpY;
                    mMarkerView.draw(canvas, pos[0], tmpY + y);
                } else {
                    mMarkerView.draw(canvas, pos[0], tmpY);
                }
            }
        }
    }
}